package com.atguigu.product.controller;

import com.atguigu.param.*;
import com.atguigu.product.service.ProductService;
import com.atguigu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassName:ProductController
 * Package:com.atguigu.product.controller
 * Description:
 *
 * @Author 应永豪
 * @Create 2024/4/21 21:05
 * @Version 1.0
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @PostMapping("promo")
    public R promo(@RequestBody @Validated ProductPromoParam param, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return R.fail("数据查询失败！");
        }

        return productService.promo(param.getCategoryName());

    }

    @PostMapping("hots")
    public R hots(@RequestBody @Validated ProductHotParam param, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return R.fail("数据查询失败！");
        }

        return productService.hots(param);
    }

    @PostMapping("category/list")
    public R clist(){
        return productService.clist();
    }

    @PostMapping("bycategory")
    public R byCategory(@RequestBody @Validated ProductIdsParam param,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return R.fail("类别商品查询失败");
        }

        return productService.byCategory(param);
    }

    @PostMapping("all")
    public R all(@RequestBody @Validated ProductIdsParam param,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return R.fail("类别商品查询失败");
        }

        return productService.byCategory(param);
    }

    @PostMapping("detail")
    public R detail(@RequestBody @Validated ProductIdParam param ,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return R.fail("商品详情查询失败");
        }

        return productService.detail(param.getProductID());

    }

    @PostMapping("pictures")
    public R pictures(@RequestBody @Validated ProductIdParam param ,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return R.fail("商品图片详情查询失败");
        }

        return productService.pictures(param.getProductID());

    }

    @PostMapping("search")
    public R search(@RequestBody ProductSearchParam param){

        return productService.search(param);
    }

}
